package com.note.chh.notebook.UI.enjoy.rank;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.note.chh.notebook.Adapter.RankListAdapter;
import com.note.chh.notebook.Api.Constant;
import com.note.chh.notebook.Model.RankListModel;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseActivity;
import com.note.chh.notebook.UI.enjoy.PlayMusicActivity;
import com.note.chh.notebook.Uitls.ItemDivider;
import com.note.chh.notebook.Uitls.UserUtil;
import com.note.chh.notebook.Uitls.gson.GsonImpl;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

//排行榜歌曲列表
public class RankListActivity extends BaseActivity {


    @BindView(R.id.mRecycle_rankList)
    RecyclerView mRecycleRankList;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    private BaseQuickAdapter rankListAdapter;
    private int Rankid = 0;
    private int pagerIndex = 1;
    private int pagerSize = 0;
    private String imageUrl = "",rankname = "";

    @Override
    protected int bindLayout() {
        return R.layout.activity_rank_list;
    }

    @Override
    public void initTopBar() {
        super.initTopBar();
        topbar.setBackgroundColor(getResources().getColor(R.color.colorTransparent));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
//234+460+218+196
    @Override
    public void initView() {
        QMUIStatusBar(RankListActivity.this);

        Bundle bundle = getIntent().getExtras();
        Rankid = bundle.getInt("Rankid");
        imageUrl = bundle.getString("imageUrl");
        rankname = bundle.getString("rankname");
        topbar.setTitle(rankname);

        mRecycleRankList.setLayoutManager(new LinearLayoutManager(this));
        mRecycleRankList.addItemDecoration(new ItemDivider().setDividerWith(SizeUtils.dp2px(0)));
        rankListAdapter = new RankListAdapter(R.layout.item_rank_list);
        rankListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mRecycleRankList.setAdapter(rankListAdapter);

        View inflate = LayoutInflater.from(this).inflate(R.layout.recycle_rank_header, null);
        ImageView mImage_pic = inflate.findViewById(R.id.mImage_pic);
        Glide.with(this).load(imageUrl).into(mImage_pic);
        rankListAdapter.addHeaderView(inflate);

    }

    @Override
    public void initListener() {
        super.initListener();
        rankListAdapter.loadMoreEnd(true);
        rankListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                pagerIndex++;
                getRankList();
            }
        },mRecycleRankList);

        rankListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!NetworkUtils.isAvailableByPing()) {//判断网络是否可用
                    showToast(RankListActivity.this,Constant.NETWORKNOUSER);
                } else if (NetworkUtils.isMobileData()) {//判断是否是移动数据  是
                    showMessageDialog(position);
                }else{//否
                    goJump(position);
                }
            }
        });
    }
    //跳转处理
    private void goJump(int position){
        List<RankListModel.SongsBean.ListBean> data = rankListAdapter.getData();
        RankListModel.SongsBean.ListBean listBean = data.get(position);
        String hash = listBean.getHash();
        Bundle bundle = new Bundle();
        bundle.putString("hash",hash);
        bundle.putBoolean("netMusic",true);
        bundle.putInt("position",position);
        bundle.putInt("songType",0);
//                bundle.putSerializable("rankList", (Serializable) data);
        bundle.putParcelableArrayList("rankList", (ArrayList<RankListModel.SongsBean.ListBean>) data);
        startActivity(PlayMusicActivity.class,bundle);
    }
    //提示网络状态
    private void showMessageDialog(final int position) {
        new QMUIDialog.MessageDialogBuilder(RankListActivity.this)
                .setTitle("提示")
                .setMessage("您当前网络不是wifi，是否继续播放音乐")
                .addAction("不了，心疼流量", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("继续，我流量多", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        goJump(position);
                        dialog.dismiss();
                    }
                })
                .show();
    }
    @Override
    public void initData() {
        getRankList();
    }

    //排行榜分类歌曲列表
    private void getRankList() {

        OkGo.<String>get("http://m.kugou.com/rank/info/?rankid=" + Rankid + "&page=" + pagerIndex + "&json=true")
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        UserUtil.dLog("-----response----"+response.body());
                        RankListModel rankListModel = GsonImpl.get().toObject(response.body(), RankListModel.class);
                        List<RankListModel.SongsBean.ListBean> list = rankListModel.getSongs().getList();
                        if (pagerIndex==1) {
                            rankListAdapter.setNewData(list);
                            pagerSize = rankListModel.getPagesize();
                        }else{
                            rankListAdapter.addData(list);
                            if (list.size()>=pagerSize) {
                                rankListAdapter.loadMoreComplete();
                            }else{
                                rankListAdapter.loadMoreEnd();
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (!NetworkUtils.isAvailableByPing()) {
                            showToast(RankListActivity.this, Constant.NETWORKNOUSER);
                        } else {
                            showToast(RankListActivity.this, Constant.UNKNOKNERROR);
                        }
                    }
                });
    }

}
